package com.tenton.controller;

import com.tenton.MyException.DataNotExist;
import com.tenton.pojo.CommodityType;
import com.tenton.service.CommodityTypeService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @Date: 2021/1/9
 * @Author: Tenton
 * @Description:
 */
@ApiIgnore
@Controller
@ApiOperation("管理员商品类型管理")
@RequestMapping("/admin/flatform/commodityType")
public class ACommodityTypeController {
    @Autowired
    CommodityTypeService commodityTypeService;

    /**
     * 跳转到增加页面
     *
     * @return
     */
    @ApiOperation("跳转到增加页面")
    @GetMapping("/toAdd")
    public String toAddType() {
        return "admin/commodity/addCommodityType";

    }

    /**
     * 先判断数据库有没有，有就更新，没有就新增
     *
     * @param commodityType
     * @param request
     * @param session
     * @return
     */
    @ApiOperation("新增商品类型")
    @PostMapping("/insertCommodityType")
    public String insertCommodityType(CommodityType commodityType, HttpServletRequest request, HttpSession session) {
        //查询此商品类型是否存在
        CommodityType flag = commodityTypeService.selectByName(commodityType.getComType());
        try {
            if (null == flag) {
                commodityTypeService.insert(commodityType,session);
                return "redirect:/admin/flatform/commodityType/getAll";
            } else {
                request.setAttribute("msg", "新增失败，系统中存在该商品类型，请重新输入！");
                return "admin/commodity/addCommodityType";
            }
        } catch (DataNotExist dataNotExist) {
            request.setAttribute("msg", "商品类型为空！");
            return "admin/commodity/addCommodityType";
        }
    }

    /**
     * 跳转到修改页面
     *
     * @param commodityType
     * @param model
     * @return
     */
    @ApiOperation("跳转到修改页面")
    @GetMapping("/toUpdate")
    public String editCommodity(CommodityType commodityType, Model model) {
        commodityTypeService.selectByPrimaryKey(commodityType.getId(), model);
        return "admin/commodity/editCommodityType";
    }

    /**
     * 修改商品类型
     *
     * @param commodityType
     * @param model
     * @param session
     * @param request
     * @return
     */
    @ApiOperation("修改商品类型")
    @PostMapping("/updateCommodityType")
    public String updateCommodityType(CommodityType commodityType, Model model,HttpSession session, HttpServletRequest request) {
        boolean flag = false;
        try {
            flag = commodityTypeService.update(commodityType,session);
            if (flag) {
                commodityTypeService.selectByPrimaryKey(commodityType.getId(), model);
                return "redirect:/admin/flatform/commodityType/getAll";
            }
        } catch (DataNotExist dataNotExist) {
            request.setAttribute("msg", "商品类型为空！");
            return "admin/commodity/addCommodityType";
        }
        return null;
    }

    /**
     * 删除商品类型
     *
     * @param id
     * @return
     */
    @ApiOperation("根据商品类型Id删除商品类型")
    @GetMapping("/deleteCommodityType")
    public String deleteCommodityType(int id) {
        commodityTypeService.deleteByPrimaryKey(id);
        return "redirect:/admin/flatform/commodityType/getAll";
    }

    /**
     * 获取所有商品类型
     *
     * @param model
     * @param pn
     * @return
     */
    @ApiOperation("获取所有商品类型")
    @GetMapping("/getAll")
    public String getAll(ModelMap model,
                         @ApiParam("页码") @RequestParam(defaultValue = "1", required = true, value = "pn") Integer pn
    ) {
        commodityTypeService.getAll(model, pn);
        return "admin/commodity/getCommodityTypeList";
    }

    /**
     * 查询商品类型
     *
     * @param id
     * @param model
     * @return
     */
    @ApiOperation("通过Id查询商品类型")
    @PostMapping("/getCommodityType")
    public String getCommodityType(int id, Model model) {
        commodityTypeService.selectByPrimaryKey(id, model);
        return "admin/commodity/getCommodityType";
    }

    /**
     * 商品类型模糊查询
     *
     * @param comType
     * @param pn
     * @param model
     * @return
     */
    @ApiOperation("商品类型模糊查询")
    @GetMapping("/getByParams")
    public String getByParams(@ApiParam("商品类型") @RequestParam(value = "comType", required = false) String comType,
                              @ApiParam("页码") @RequestParam(defaultValue = "1", required = true, value = "pn") Integer pn,
                              ModelMap model
    ) {
        commodityTypeService.getByParams(comType, pn, model);
        return "/admin/commodity/getCommodityTypeByParams";
    }
}
